<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class T_follow_user extends MY_Model{
	public function __construct()
	{
		parent::__construct();
		$this->table_name = T_FOLLOW_USER;
	}

	/**
	 * get user by id
	 * @param  string  $select : fields of table need get
	 * @param  integer $id     id of user
	 * @return array|null
	 */
	public function get_data_by_id($id = -1, $select = '*'){
		if($id == -1 || !is_numeric($id)){
			return null;
		}

		if(strcmp($select, '*') == 0){
			$this->db->select();
		}else{
			$this->db->select($select);
		}

		$this->db->where(FL_USER_ID ." = $id LIMIT 1");

		$result = $this->db->get($this->table_name);
		$result = $result->result_array();
		return $result;
	}

	/**
	 * get data by property
	 * @param  string $select : fields of table need get
	 * @param  array  $where  conditions
	 * @return array|null
	 */
	public function get_data_by_property($where = array(), $select = '*'){
		if($where == null || !is_array($where)){
			return null;
		}

		// query
		if(strcmp($select, '*') == 0){
			$this->db->select();
		}else{
			$this->db->select($select);
		}
		$this->db->where($where);
		$result = $this->db->get($this->table_name);
		$result = $result->result_array();
		return $result;
	}

	/**
	 * insert data into talbe
	 * @param array   $data   array data insert
	 * @param integer $result if 0 return id inserted else 1 return data user inserted
	 * @return null|integer|array
	 */
	public function set_data($data = array(), $result = 0){
		if($data == null || !is_array($data)){
			return null;
		}

		// insert rows
		$this->db->insert($this->table_name, $data);
		// get id
		$last_id = $this->db->insert_id();
		//return
		if($result == 0){
			return $last_id;
		}else{
			return $this->get_data_by_id($last_id);
		}
	}

	/**
	 * update data by id
	 * @param  array   $data : data update
	 * @param  integer $id
	 * @return integer
	 */
	public function update_data_by_id($data = array(), $id = -1){
		if($data == null || !is_array($data) || $id == -1 || !is_numeric($id)){
			return null;
		}

		//condition
		$this->db->where(FL_USER_ID ." = $id LIMIT 1");
		$this->db->update($this->table_name, $data);
		return $this->db->affected_rows();
	}

	/**
	 * update data
	 * @param  array  $data
	 * @param  array  $where : conditions update
	 * @return integer
	 */
	public function update_data_by_property($data = array(), $where = array()){
		if($data == null || !is_array($data) || $where == null || !is_array($where)){
			return null;
		}

		$this->db->where($where);
		$this->db->update($this->table_name, $data);
		return $this->db->affected_rows();
	}

	public function get_user_following($userId = -1, $receive = 1, $page = -1, $per_page = -1){
		if($userId == -1 || !is_numeric($userId)){
			return null;
		}
		$this->db->select();
		$this->db->from($this->table_name);
		$this->db->where(array(FL_USER_FROM_ID=>$userId, FL_USER_GET_NOTI => $receive));
		$this->db->join(T_USERS, T_USERS.'.'.USER_ID .'='. $this->table_name.'.'.FL_USER_TO_ID);
		if($page > -1){
			$this->db->limit($per_page, $page*$per_page);
		}
		$query = $this->db->get();
		$query = $query->result_array();
		return $query;
	}

	public function get_user_followed($userId = -1, $receive = 1){
		if($userId == -1 || !is_numeric($userId)){
			return null;
		}

		$query = $this->db->query("select f.*,u.".USER_CREATE_DATE.",u.".USER_ADDRESS.",u.".USER_RATE.",u.".USER_NAME.",u.".USER_AVATAR_PATH." from ". T_USERS ." as u join (select a.* , b.".FL_USER_UNFOLLOW." as st_IsUnFollow from ".$this->table_name. " as a left join ".$this->table_name." as b on a.".FL_USER_FROM_ID." = b.".FL_USER_TO_ID." and a.".FL_USER_TO_ID." = b.".FL_USER_FROM_ID." where a.".FL_USER_TO_ID." = $userId and a.".FL_USER_UNFOLLOW." = 0 ) as f on u.".USER_ID." = f." .FL_USER_FROM_ID );

		$query = $query->result_array();
		return $query;
	}

	public function get_user_followed_by_id($userTo = -1, $userFrom = -1){
		if($userTo == -1 || !is_numeric($userTo) || $userFrom == -1 || !is_numeric($userFrom)){
			return null;
		}

		$query = $this->db->query("select T1.st_FollowDate,T1.st_FollowId,T2.st_unfollow as st_IsUnFollow, userinfo.*
FROM (SELECT st_fromuserid,st_FollowDate,st_FollowId from follow_user where st_touserid=$userTo and st_fromuserid!=$userFrom) AS T1 
 LEFT JOIN (select st_touserid,st_unfollow from follow_user where st_fromuserid=$userFrom) AS T2 ON T1.st_fromuserid=T2.st_touserid JOIN userinfo ON userinfo.st_userid=T1.st_fromuserid");
		$query = $query->result_array();
		return $query;
	}
}